//
//  RequestAPIExample.m
//  Growth
//
//  Created by Hobo.Chen on 1/5/12.
//  Copyright 2011 MEETStudio. All rights reserved.
//

#import "RequestAPIExample.h"

#import "Config.h"

@implementation RequestAPIExample


#pragma mark log
+ (NSMutableURLRequest *)login_openRequestWithUsr:(NSString *)usr 
										  pwd:(NSString *)pwd{
	
	//NSString *urlString = [NSString stringWithFormat:@"%@/login.xml",BASEURL];
    NSString *urlString = [NSString stringWithFormat:@"%@/j_spring_security_open_check",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
    
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    
	NSString *httpBodyString = [NSString stringWithFormat:@"j_username=%@&j_password=%@",usr,pwd];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
    //  CLog(@"header %@", [(NSMutableURLRequest *)request allHeaderFields]);
    
    
    CLog(@"URL %@", urlString);
    CLog(@"body %@",httpBodyString );
	return request;
}






+ (NSMutableURLRequest *)logginRequestWithUsr:(NSString *)usr 
										  pwd:(NSString *)pwd{
	
	//NSString *urlString = [NSString stringWithFormat:@"%@/login.xml",BASEURL];
    NSString *urlString = [NSString stringWithFormat:@"%@/j_spring_security_check",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
    
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
       
    
	NSString *httpBodyString = [NSString stringWithFormat:@"j_username=%@&j_password=%@",usr,pwd];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
  //  CLog(@"header %@", [(NSMutableURLRequest *)request allHeaderFields]);
   
    
    CLog(@"URL %@", urlString);
    CLog(@"body %@",httpBodyString );
	return request;
}

+ (NSMutableURLRequest *)registerRequestWithEmail:(NSString *)email  
											  pwd:(NSString *)pwd  usr:(NSString *)usr{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/register.xml",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
	
	NSString *httpBodyString = [NSString stringWithFormat:@"email=%@&pwd=%@&usr=%@",email,pwd,usr];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
	return request;
}

#pragma mark user
+ (NSMutableURLRequest *)requestUserWithUserID:(NSString *)userid  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/user/%@/show.xml?skey=%@",BASEURL,userid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)requestFriendListWithUserID:(NSString *)userid  pid:(NSString *)pid
												  ps:(NSString *)ps     skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/list.xml?userId=%@&pid=%@&ps=%@&skey=%@",
						   BASEURL,userid,pid,ps,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

#pragma mark note
+ (NSMutableURLRequest *)noteRequestWithNoteID:(NSString *)noteid  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/show.xml?skey=%@",BASEURL,noteid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)shareNoteListWithUID:(NSString *)uid  pid:(NSString *)pid 
										   ps:(NSString *)ps  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/share/list.xml?userId=%@&pid=%@&ps=%@&skey=%@",BASEURL,uid,pid,ps,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//已分享好友id list
+ (NSMutableURLRequest *)noteSharedUserIDListWith:(NSString *)noteid skey:(NSString *)skey
{
	
	//$domain/note/${noteId}/shared_uids.xml
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/shared_uids.xml?skey=%@",BASEURL,noteid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
	
}

+ (NSMutableURLRequest *)hotNoteListWithUID:(NSString *)uid  pid:(NSString *)pid 
										 ps:(NSString *)ps  skey:(NSString *)skey  hotType:(NSString *)type{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/hot/list.xml?userId=%@&pid=%@&ps=%@&skey=%@&hotType=%@",
						   BASEURL,uid,pid,ps,skey,type];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)nearNoteListWithUID:(NSString *)uid  pid:(NSString *)pid 
										  ps:(NSString *)ps  skey:(NSString *)skey  x:(float)x_gps  y:(float)y_gps{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/near/list.xml?userId=%@&pid=%@&ps=%@&x=%f&y=%f&skey=%@",BASEURL,uid,pid,ps,x_gps,y_gps,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

#pragma mark comment
+ (NSMutableURLRequest *)commentListWithNoteID:(NSString *)noteid  pid:(NSString *)pid
											ps:(NSString *)ps     skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/comment/%@/list.xml?&pid=%@&ps=%@&skey=%@",BASEURL,noteid,pid,ps,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

#pragma mark action
+ (NSMutableURLRequest *)likeWithNoteID:(NSString *)noteid    skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/like.xml?skey=%@",BASEURL,noteid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)shareWithNoteID:(NSString *)noteid   skey:(NSString *)skey
									uids:(NSString *)uids{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/share.xml?uids=%@&skey=%@",
						   BASEURL,noteid,uids,skey];
	NSLog(@"share url %@", urlString);
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
	
}

//取消分享
+ (NSMutableURLRequest *)cancelShareWithNoteID:(NSString *)noteid uids:(NSString *)uids skey:(NSString *)skey
{
	//$domain/note/${noteId}/unshare.xml?skey=***
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/unshare.xml?uids=%@&skey=%@",
						   BASEURL,noteid,uids,skey];
	NSLog(@"share url %@", urlString);
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)addFriendWithUid:(NSString *)uid     skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/add.xml?uid=%@&skey=%@",
						   BASEURL,uid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//accept friend
+ (NSMutableURLRequest *)acceptFriendWithUids:(NSString *)uids    skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/accept.xml?uids=%@&skey=%@",
						   BASEURL,uids,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//reject friend
+ (NSMutableURLRequest *)rejectFriendWithUids:(NSString *)uids    skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/reject.xml?uids=%@&skey=%@",
						   BASEURL,uids,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//look for friend :  site:qq/sina/renren
+ (NSMutableURLRequest *)lookForFriendWithName:(NSString *)name    site:(NSString *)site
										  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/%@/friend/list.xml?names=%@&skey=%@",
						   BASEURL,site,name,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)deleteFriendWithUid:(NSString *)uid  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/delete.xml?uid=%@&skey=%@",
						   BASEURL,uid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

+ (NSMutableURLRequest *)forgetPasswordWithEmail:(NSString *)email  
											skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/password/forget.xml?email=%@&skey=%@",
						   BASEURL,email,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//apply to be friend list
+ (NSMutableURLRequest *)applyFriendListWithPid:(NSString *)pid    ps:(NSString *)ps
										   skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/friend/apply_list.xml?pid=%@&ps=%@&skey=%@",
						   BASEURL,pid,ps,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

//collect
+ (NSMutableURLRequest *)collectWithNoteID:(NSString *)noteid    skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/note/%@/collect.xml?skey=%@",
						   BASEURL,noteid,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

#pragma mark search
+ (NSMutableURLRequest *)searchUserWithWord:(NSString *)word   pid:(NSString *)pid
										   ps:(NSString *)ps      skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/search/user.xml",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
	
	NSString *httpBodyString = [NSString stringWithFormat:@"word=%@&pid=%@&ps=%@&skey=%@",word,pid,ps,skey];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
	return request;
}

+ (NSMutableURLRequest *)searchTagWithWord:(NSString *)word   pid:(NSString *)pid
										  ps:(NSString *)ps      skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/search/tag.xml",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
	
	NSString *httpBodyString = [NSString stringWithFormat:@"word=%@&pid=%@&ps=%@&skey=%@",word,pid,ps,skey];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
	return request;
}

+ (NSMutableURLRequest *)searchNoteWithWord:(NSString *)word   pid:(NSString *)pid
										   ps:(NSString *)ps      skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/search/note.xml",BASEURL];
	//转换UTF8编码
	NSString *urlStringUTF8 = [NSString stringWithUTF8String:[urlString UTF8String]];
	NSURL *url = [NSURL URLWithString:urlStringUTF8];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
	
	NSString *httpBodyString = [NSString stringWithFormat:@"word=%@&pid=%@&ps=%@&skey=%@",word,pid,ps,skey];
	[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	
	return request;
}

#pragma mark sync
// data:category/note/tag/attachment;  zip:true/false;
+ (NSMutableURLRequest *)syncRequestWithData:(NSString *)data    lastSyncTime:(NSString *)lastTime
										 zip:(NSString *)zipbool  skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/sync/%@/request.xml?last_sync_time=%@&zip=%@&skey=%@",
						   BASEURL,data,lastTime,zipbool,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

// data:category/note/tag/attachment;  zip:true/false;
+ (NSMutableURLRequest *)syncUploadWithXMLData:(NSData *)data   zip:(NSString *)zipbool    
										  skey:(NSString *)skey    data:(NSString *)type{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/sync/upload.xml",BASEURL];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	[request setHTTPMethod:@"POST"];
	
	//http data
	NSMutableData *httpData = [NSMutableData data];
	NSString *httpBodyString = [NSString stringWithFormat:@"zip=%@&data=%@&skey=%@&xml=",zipbool,type,skey];
	
	//NSString *httpBodyString = [NSString stringWithFormat:@"zip=%@&data=%@&skey=%@",zipbool,type,skey];
	
	[httpData appendData:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]];
	[httpData appendData:data];
	
	[request setHTTPBody:httpData];
	
	//NSLog(@"http body: %@ \n %@",httpBodyString,[request HTTPBody]);
	
	return request;
}

// zip:true/false
+ (NSMutableURLRequest *)syncUserRequestWithZip:(NSString *)zipbool    skey:(NSString *)skey{
	
	NSString *urlString = [NSString stringWithFormat:@"%@/sync/user/request.xml?skey=%@&zip=%@",
						   BASEURL,zipbool,skey];
	NSURL *url = [NSURL URLWithString:urlString];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
													   timeoutInterval:kWebRequestTimeoutInterval];
	
	return request;
}

// request resource,直接取得图片文件
+ (NSMutableURLRequest *)syncResourceWithNoteID:(NSString *)noteid   name:(NSString *)filename{
	return nil;
}

@end
